package Client.ui;


import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JButton;

import VWorld.Avatar;

import Client.Controler.AppliManager;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class DialogWindow {

	private JFrame frame;
	
	private AppliManager appliManager;

	private Avatar sendTo;

	private JTextArea taMessages;

	private JTextArea taMessageToSend;

	/**
	 * Create the application.
	 */
	public DialogWindow(AppliManager pappliManager, Avatar psendTo) {
		this.appliManager = pappliManager;
		this.sendTo = psendTo;
		initialize();
		this.frame.setVisible(true);
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.addWindowListener(new WindowAdapter() {

			@Override
			public void windowClosed(WindowEvent e) {
				appliManager.close();
			}
		});
		frame.setBounds(100, 100, 686, 428);
		frame.setTitle(this.sendTo.name);
		frame.getContentPane().setLayout(null);
		
		JLabel nameOfTheCorrespondant = this.getNameOfTheCorrespondant();
		frame.getContentPane().add(nameOfTheCorrespondant);
		
		this.taMessageToSend = new JTextArea();
		taMessageToSend.setBounds(10, 279, 551, 102);
		frame.getContentPane().add(taMessageToSend);
		
		JButton btnNewButton = this.getBTNNewButton();
		frame.getContentPane().add(btnNewButton);
		
		this.taMessages = new JTextArea();
		taMessages.setBounds(10, 36, 650, 232);
		taMessages.setEditable(false);
		frame.getContentPane().add(taMessages);
	}

	private JButton getBTNNewButton() {
		JButton btnNewButton = new JButton("Send");
		btnNewButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				appliManager.sendMessageTo(sendTo, taMessageToSend.getText());
				taMessageToSend.setText("");
				taMessages.setText(taMessages.getText()+ "\n"+ appliManager.getName()+": "+taMessageToSend.getText());
			}
		});
		btnNewButton.setBounds(571, 279, 89, 102);
		return btnNewButton;
	}

	private JLabel getNameOfTheCorrespondant() {
		JLabel nameOfTheCorrespondant = new JLabel(this.sendTo.name);
		nameOfTheCorrespondant.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				new CreateAvatarWindow(appliManager, sendTo, false);
			}
		});
		nameOfTheCorrespondant.setBounds(10, 11, 214, 14);
		return nameOfTheCorrespondant;
	}

}
